<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="org.teammach.laddercomp.dao.LadderManager"%>
<%@ page import="org.teammach.laddercomp.dao.BlogManager"%>
<%@ page import="org.teammach.laddercomp.dao.CommentManager"%>


<%@ page import="org.teammach.laddercomp.shared.Ladder"%>
<%@ page import="org.teammach.laddercomp.shared.OfficialLadder"%>
<%@ page import="org.teammach.laddercomp.shared.Team"%>
<%@ page import="org.teammach.laddercomp.shared.BlogEntry"%>
<%@ page import="org.teammach.laddercomp.shared.Comment"%>

<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>

<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="java.text.SimpleDateFormat"%>


<%@ page import="java.util.List"%>
<%@ page import="java.util.TimeZone"%>


<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css">
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-23998388-1']);
 _gaq.push(['_trackPageview']);

 (function() {
   var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
 })();

</script>
</head>
<body>
<div id="wrapper">

<%
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	LadderManager manager = LadderManager.INSTANCE;
%>
<a href="/">
<span id="header">
</span>
</a>
<div id="page">
<div style="float:left;width:480px">
<div id="content">
<h1 class="title">Standings</h1>

<table width="90%">
	<tr>
		<th width="5%"> </th>
		<th width="20%">Team name</th>
		<th width="10%">Score</th>
		<th width="65%">Haydn's wise words</th>
	</tr>
	<%
		List<Ladder> ladders = manager.listLadders();
		for (int i = 0; i < ladders.size(); i++) {
			if (ladders.get(i).getUser().startsWith("Official")) {
				continue;
			}
	%>
	<tr <%if (i % 2 == 0) {%> class="alt" <%}%>>
		<td width="5%"><%=i%></td>
		<td width="20%"><a
			href="ladder.jsp?user=<%=ladders.get(i).getUser()%>"><%=ladders.get(i).getUser()%></a></td>
		<td width="10%" align="center"><%=ladders.get(i).getCurrentScore()%></td>
		<td width="65%"><%=ladders.get(i).getCommentary()%></td>
	</tr>
	<%
		}
	%>

</table>
<%
	OfficialLadder official = manager.getLatestOfficial();
	SimpleDateFormat sdf = new SimpleDateFormat(
			"EEE, d MMM yyyy hh:mmaaa");
	sdf.setTimeZone(TimeZone.getTimeZone("GMT+10"));
%><div  style="text-align:center;color:#646464"> Ladder last updated : <%=sdf.format(official.getUpdated())%></div></div>
	<div id="content">
		<h1 class="title">Comments</h1>
		
		<div  style="text-align:left;padding:15px">
		<%List<Comment> comments = CommentManager.INSTANCE.getComments(0,5);
			for(Comment comment: comments) {
		%> 
		<h3><%=comment.getUser().getNickname() %></h3>
		<%=comment.getContent() %>
		<%if(comment.getUser().equals(user)){ %><p><a href="comments.jsp?action=delete&key=<%=KeyFactory.keyToString(comment.getKey()) %>">Delete Comment</a></p>  <%}%>
		<p style="text-align:right"><%= sdf.format(comment.getCreated())%></p>
		<hr></hr>
		<%} %>
				<p style="text-align:right"><a href="comments.jsp?page=0">SEE ALL COMMENTS</a></p>
		</div>
		
		<div  style="text-align:center;margin-top:-10px;padding:5px">
		<% if(user != null) { %>
		<FORM METHOD=POST ACTION="comments.jsp">
			<h1 class="title">Add new comment</h1>

			<textarea name="comment" rows="3" cols="60" style="margin-left:10px" ></textarea>
				<input type="hidden" name="action" value="add">
			<P><INPUT TYPE=SUBMIT style="float:right;margin-right:10px">
		</FORM> 
		
		<%} else{%>
		<p>You must be logged in to leave a comment. <a href="<%=userService.createLoginURL(request.getRequestURI())%>">Login</a></p>
		<%} %>
		</div>
	</div>
	</div>
<div style="float:right;display:block;width:250px">

<div id="sidebar">
<h1 class="title">Latest</h1>



	<div  style="text-align:center;margin-top:-10px;padding:5px">
		<%
			BlogManager blogManager = BlogManager.INSTANCE;
			BlogEntry latest = blogManager.getLatestBlog();
			if (latest == null) {
		%>
		No blog entries have been recorded so far.
		<%
			} else {
				if(latest.getContent().length() < 500) {
		%>
		<a href="blog.jsp"><%=latest.getContent()%></a>
		<%
				}else {
					%>
					<a href="blog.jsp" style="text-decoration:none"><%=latest.getContent().substring(0,497)%>...</a>
					<%
				}
			}
		%>
	</div>
	</div>


<div id="sidebar">
<h1 class="title">Current Ladder</h1>
<table width="90%">
	<tr>
		<th>Team</th>
		<th>P</th>
		<th>W</th>
		<th>L</th>
		<th>D</th>
		<th>PTS</th>
	</tr>
	<%
		int i = 0;
		for (Team team : manager.getTeams()) {
	%>
	<tr<%if (i++ % 2 == 0) {%> class="alt" <%}%>>
		<td><%=team.getTeamName()%></td>
		<td><%=team.getPlayed()%></td>
		<td><%=team.getWon()%></td>
		<td><%=team.getLost()%></td>
		<td><%=team.getDrawn()%></td>
		<td><%=team.getPoints()%></td>
	</tr>
	<%
		}
	%>


	</table>

	</div>
	

	
	<div id="sidebar">
<h1 class="linksTitle">Links</h1>



	<div  style="text-align:center;margin-top:-10px;padding:5px">
		<ul>
			<li><a href="standings.jsp">Current Standings</a></li>
			<li><a href="rules.jsp">Rules and regulations</a> </li>
			<li><a href="blog.jsp">Haydn's weekly blog</a> </li>
			<li><a href="mailto:dscerri@gmail.com">Contact Webmaster</a> </li>
				<%
		if (user != null
				&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
						.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
	%>
	 <li><a href="update.jsp">Update ladder</a></li> 
	<li><a href="<%=userService.createLogoutURL(request.getRequestURI())%>">Log out</a>	</li><%
		} else {
	%>
	<li><a href="<%=userService.createLoginURL(request.getRequestURI())%>">Admin login</a></li>
	<%
		}
	%>
		</ul>
	</div>
	</div>

			
	</div>
	</div>
	
	</div>
	<div id="footer">
	<p>&copy;2012 All Rights Reserved &nbsp;&bull;&nbsp; Design by Dave
	Scerri Productions &nbsp;&bull;&nbsp; CSS Template from <a
		href="http://www.freecsstemplates.org/">Free CSS Templates</a> .</p>
	</div>
</body>
</html>